package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.Dishservice;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishserviceImpl implements Dishservice {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    @Transactional
    public void saveWhitFlavor(DishDTO dishDTO) {
        //向菜品表插入菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //数据库插入完成后返回主键id，获取id
        Long id = dish.getId();

        //向口味表插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!flavors.isEmpty()){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */

    @Override
    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {
      PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
      Page<DishVO> page=dishMapper.PageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 删除菜品
     */
    @Transactional
    @Override
    public void DeleteBatch(List<Long> ids) {
        //菜品是否可以删除，启售中，套餐关联
        ids.forEach(id -> {
            Dish dish=dishMapper.getByID(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        List<Long> setmealDishID = setmealMapper.getSetmealDishID(ids);
        if (!setmealDishID.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品中的数据
        //删除菜品关联的口味数据
        ids.forEach(id->{
            dishMapper.DeleteByID(id);
            dishFlavorMapper.DeleteByID(id);
        });

    }

    /**
     * 更新菜品启售状态
     * @param status
     * @param id
     */
    @Override
    public void updateDishStatus(String status, String id) {
        Dish dish = new Dish();
        dish.setStatus(Integer.valueOf(status));
        dish.setId(Long.valueOf(id));
        dishMapper.updateDish(dish);
    }

    /**
     * 根据id查询菜品
     * @return
     */
    @Override
    public DishVO getByIDWithFlaover(Long id) {
        Dish dish = dishMapper.getByID(id);
        List<DishFlavor> dishFlavor=dishFlavorMapper.getByidWhitFlaover(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    /**
     * 更新菜品
     * @param dishDTO
     */
    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //更新菜品信息
        dishMapper.updateDish(dish);
        //删除菜品口味数据
        dishFlavorMapper.DeleteByID(dish.getId());
        if (!dishDTO.getFlavors().isEmpty()){
            //添加菜品口味数据
            dishDTO.getFlavors().forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.insertBatch(dishDTO.getFlavors());
        }
    }
}
